package com.rabbiter.oes.controller;

import com.rabbiter.oes.entity.*;
import com.rabbiter.oes.result.R;
import com.rabbiter.oes.service.impl.FillQuestionServiceImpl;
import com.rabbiter.oes.service.impl.JudgeQuestionServiceImpl;
import com.rabbiter.oes.service.impl.MultiQuestionServiceImpl;
import com.rabbiter.oes.service.impl.PaperServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "试卷与试题对应关系接口")
@RestController
public class PaperController {

    @Autowired
    private PaperServiceImpl paperService;

    @Autowired
    private JudgeQuestionServiceImpl judgeQuestionService;

    @Autowired
    private MultiQuestionServiceImpl multiQuestionService;

    @Autowired
    private FillQuestionServiceImpl fillQuestionService;

    @ApiOperation(value = "所有试卷试题关系信息获取", notes = "所有试卷试题关系信息获取", httpMethod = "GET")
    @GetMapping("/papers")
    public R<List<PaperManage>> findAll() {
       return R.<List<PaperManage>>ok("请求成功").data(paperService.findAll());
    }

    @ApiOperation(value = "更具试卷编号获取试卷题目相关信息", notes = "更具试卷编号获取试卷题目相关信息", httpMethod = "GET")
    @GetMapping("/paper/{paperId}")
    public Map<Integer, List<?>> findById(@PathVariable("paperId") Integer paperId) {
        List<MultiQuestion> multiQuestionRes = multiQuestionService.findByIdAndType(paperId);   //选择题题库 1
        List<FillQuestion> fillQuestionsRes = fillQuestionService.findByIdAndType(paperId);     //填空题题库 2
        List<JudgeQuestion> judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId);   //判断题题库 3
        Map<Integer, List<?>> map = new HashMap<>();
        map.put(1,multiQuestionRes);
        map.put(2,fillQuestionsRes);
        map.put(3,judgeQuestionRes);
        return map;
    }

    @ApiOperation(value = "添加试卷与题目对应关系", notes = "添加试卷与题目对应关系", httpMethod = "POST")
    @PostMapping("/paperManage")
    public R add(@RequestBody PaperManage paperManage) {
        int res = paperService.add(paperManage);
        if (res != 0) {
            return R.ok("添加成功").data(res);
        }
        return R.logicError("添加失败").data(res);
    }

    @ApiOperation(value = "删除试卷中的某条试题", notes = "删除试卷中的某条试题", httpMethod = "GET")
    @GetMapping("/paper/delete/{paperId}/{type}/{questionId}")
    public R delete(
            @PathVariable("paperId") String paperId,
            @PathVariable("type") String type,
            @PathVariable("questionId") String questionId
    ) {
        paperService.delete(paperId, type, questionId);
        return R.ok("删除成功");
    }
}
